#!/usr/bin/env python3
# -*- coding:utf-8 -*-  

'''
This module is used to retrieve the modules to be packaged from the configuration file and redistribute them according to the given file structure.
'''

from openpyxl import load_workbook
import os
import os.path

class PathList():
    
    def __init__(self,files_path='filespath.xlsx'):
        self._filespath = files_path
        self._path_list = []
        self._getFilesPath()

    def getPathList(self):
        return self._path_list

    def _getFilesPath(self):
        '''
        '''
        wb = load_workbook(self._filespath)
        ws = wb['Sheet1']
        
        for row in list(ws.rows)[2:]:
            if row[0].value:
                new_file = {}
                new_file['index'] = row[0].value
                s_path = row[1].value
                if os.sys.platform == 'linux':
                    if os.path.isdir(s_path):
                        new_file['s_file'] = ''
                        new_file['s_dir'] = s_path
                    else:
                        new_file['s_file'] = os.path.basename(s_path)
                        new_file['s_dir'] = os.path.dirname(s_path)
                else:
                    new_file['s_file'] = os.path.basename(s_path)
                    new_file['s_dir'] = os.path.dirname(s_path)
                new_file['d_path'] = row[2].value
                new_file['t_path'] = row[3].value

                self._path_list.append(new_file)

        dir_set = set()
        # find the rootest dir.
        remove_dir_list = []
        for ever_path in self._path_list:
            if ever_path['t_path'] == 'dir':
                cur_dir = ever_path['s_dir']
                for ever_dir in dir_set:
                    if len(cur_dir) < len(ever_dir) and cur_dir == ever_dir[:len(cur_dir)]:
                        remove_dir_list.append(ever_dir)
                        dir_set.remove(ever_dir)
                        dir_set.add(cur_dir)
                        break
                dir_set.add(cur_dir)
        
        # remove all the path included in upper dir.
        for ever_path in self._path_list[:]:
            if ever_path['s_dir'] in remove_dir_list:
                self._path_list.remove(ever_path)

        for path in self._path_list:
            if path['t_path'] == 'dir':
                for root, dirs, files in os.walk(path['s_dir']):
                    for name in files:
                        new_file = {}
                        new_file['index'] = -1
                        new_file['s_file'] = name
                        new_file['s_dir'] = root
                        if root.find('.') != -1:
                            continue
                        new_file['d_path'] = os.path.join(path['d_path'],root[len(path['s_dir'])+1:])
                        new_file['t_path'] = 'file' 

                        for ever_path in self._path_list:
                            if ever_path['s_file'] == new_file['s_file'] and ever_path['s_file'] == new_file['s_file']:
                                continue
                        self._path_list.append(new_file)

if __name__ == "__main__":
    pl = PathList('filespath.xlsx')
    for path in pl.getPathList():
        print(path)



